"use strict"
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";

const routes: Array<RouteRecordRaw> = [
    {
        path: "/",
        name: "home",
        component: () => import("@/pages/Home.vue"),
        meta: {
            id: "home",
            title: "home",
            auth: true,
            icon: "home",
        },
    },
    {
        path: "/console",
        name: "console",
        component: () => import("@/pages/Console.vue"),
        meta: {
            id: "console",
            title: "console",
            auth: true,
            icon: "console",
        },
        children: []
    },
    {
        path: "/dashboard",
        name: "dashboard",
        component: () => import("@/pages/Dashboard.vue"),
        meta: {
            id: "dashboard",
            title: "dashboard",
            auth: true,
            icon: "dashboard",
        },
        children: []
    },
    {
        path: "/account",
        name: "account",
        component: () => import("@/pages/Account.vue"),
        redirect:"/account/login",
        meta: {
            id: "account",
            title: "account",
            icon: "account",
            auth: false
        },
        children: [
            {
                path: "login",
                name: "account-login",
                component: () => import("@/components/account/Login.vue"),
                meta: {
                    id: "account-login",
                    title: "account-login",
                },
            },
            {
                path: "register",
                name: "account-register",
                component: () => import("@/components/account/Register.vue"),
                meta: {
                    id: "account-register",
                    title: "account-register",
                }
            },
            {
                path: "verify",
                name: "account-verify",
                component: () => import("@/components/account/Verify.vue"),
                meta: {
                    id: "account-verify",
                    title: "account-verify",
                }
            },
            {
                path: "forget",
                name: "account-forget",
                component: () => import("@/components/account/Forget.vue"),
                meta: {
                    id: "account-forget",
                    title: "account-forget",
                }
            },
        ]
    },
    {
        path: "/helper",
        name: "helper",
        component: () => import("@/pages/Helper.vue"),
        meta: {
            id: "helper",
            title: "helper",
            icon: "helper",
            auth: false
        },
        children: []
    },
    {
        path: "/welcome",
        name: "welcome",
        component: () => import("@/pages/Welcome.vue"),
        meta: {
            id: "welcome",
            title: "welcome",
            auth: false,
            icon: "welcome",
        },
        children: []
    },
    {
        path: "/:w+",
        name: "*",
        component: () => import("@/pages/Notfound.vue"),
        meta: {
            id: "notfound",
            title: "notfound",
            auth: false
        },
    }
]
const AppRouter = createRouter({
    history: createWebHistory(),
    routes,
});
function getLoginState() {
    let token = localStorage.getItem("token")
    return !!(token && token.length > 10);
}
AppRouter.beforeEach((to, from, next) => {
    console.log(to, from)
    if (getLoginState()) next()
    else if (!getLoginState() && to.meta.auth) next("/account")
    else next()
})
export default AppRouter;